<?php

class Login_Model extends Model
{
    
    public function __construct()
    {
        parent::__construct();
    }
    
    public function login()
    {
        $sth = $this->db->prepare('select id, role from users where login=:login and password=:password');
        
        $sth->execute(array(
            ':login' => $_POST['login'],
            ':password' => Hash::create('md5', $_POST['password'], HASH_PASSWORD_KEY)
        ));
        
        $data = $sth->fetch();

        $count = $sth->rowCount();
        
        if($count > 0) 
        {
            Session::init();

            try{
                Session::set('role', $this->role[ $data['role'] ]);
            } catch(Exception $e) {
    
                echo 'somsing wrong';
                exit();
            }


            Session::set('log_in', true);
            header('Location: ../dashboard');
        } else {
            // @todo: set error
            header('Location: ../login');
        }
    }
    
    
    public function logout()
    {
        Session::destroy();
        header('Location: ../login');
    }
}
